#include "AnaDef.h"
#include "XGLUtils.h"
#include "style.h"

const Int_t fsty[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//3105,3195,3105,3195,3105,3195,3105,3195,3105,3195};//3004,3005,3006,3007,3004,3005,3006,3007,3004,3005,3006,3007};//3001;//3005;//3002;//3013;
const Double_t lgmar = 0.15;//0.05;
TString types[]={"p","#pi","K","e"};
const TString ps[]={"p","#pi^{+}","K^{+}","e^{+}"};
const TString ns[]={"#bar{p}","#pi^{-}","K^{-}","e"};
TString outname[]={"proton","pion","kaon","electron"};

//const Int_t col[]={kBlue, kRed, kGreen+3, kMagenta};
Bool_t fklogx=kTRUE;
Bool_t fklogy=kFALSE;
Int_t fkETA=999;
Int_t fkjet=0;
Int_t fgrid = -999;
Int_t fkpar = -999;
Bool_t fkTheory = kFALSE;
Bool_t fkcmpjpt = kFALSE;

TString ftag;

TList * glist = 0x0;

Int_t gETA = -999;

const Int_t fColBl=kBlue;//7;//

Int_t IDx()
{
  return (fgrid/10)%5;
}

Int_t IDy()
{
  return fgrid%10;
}

Char_t * SysName(const Int_t itype)
{
  return Form("grsys%d%s",itype, gETA>=0?Form("Eta%d", gETA):"");
}

Char_t * StatName(const Int_t itype)
{
  return Form("grstat%d%s",itype, gETA>=0?Form("Eta%d", gETA):"");
}

TString GetGr(const Int_t ipar, const TString fin, TList *ll)
{
  TFile *ff = new TFile(fin); 
  if(!ff){
    printf("no ff!! %s\n", fin.Data()); exit(1);
  }

  const TString pwd=gSystem->pwd();

  const Bool_t kNoErr = kFALSE; //fin.Contains("MCFF");
  printf("\n\n******************************** kNoErr %d *******************************\n\n\n", kNoErr);

  TString spar=Form("%s%s in ",types[ipar].Data(), IDy()==3 || IDy()==7 ?Form("/%s",types[1].Data()):"" );
  if(IDy()==6){
    spar.ReplaceAll("K","K^{+}+K^{-}");
    spar.ReplaceAll("#pi","#pi^{+}+#pi^{-}");
    if(ipar==0){
      spar.ReplaceAll("p","p+#bar{p}");
    }
  }
  else{
    spar.ReplaceAll("K","(K^{+}+K^{-})");
    spar.ReplaceAll("#pi","(#pi^{+}+#pi^{-})");
    spar.ReplaceAll("p/","(p+#bar{p})/");
  }

  TString year;
  if(fin.Contains("10h")){
    if(fin.Contains("CEN0"))
      year = "10h 0-5%";
    else if(fin.Contains("CEN1"))
      year = "10h 5-10%";
    else if(fin.Contains("CEN2"))
      year = "10h 10-20%";
    else if(fin.Contains("CEN3"))
      year = "10h 20-40%";
    else if(fin.Contains("CEN4"))
      year = "10h 40-60%";
    else if(fin.Contains("CEN5"))
      year = "10h 60-80%";
    else if(fin.Contains("CEN6"))
      year = "10h 80-100%";
  }
  else if(fin.Contains("11h")){
    year = "11h";
  }
  else if(fin.Contains("10e")){
    year = "10e";
  }
  else if(fin.Contains("10-d-e") || pwd.Contains("10-d-e") || fin.Contains("10de") || pwd.Contains("10de")){
    year = "pp #sqrt{s}=7 TeV anti-#it{k}_{T} #it{R}=0.4";
    if(ftag.Contains("cmpJpt")){
      year = "pp #sqrt{s}=7 TeV";
    }

    TString idtag(fin);
    idtag.ToUpper();
    if(idtag.Contains("ID6") || idtag.Contains("ID7") || idtag.Contains("ID8") || fgrid/10>=5 ){ 
      if(fin.Contains("directMC")){
        //year+= ", MC truth";
        year+=", PYTHIA Perugia0 det. truth";
      }
      else{
        year+=", MC perfect PID";
      }
    }
    else if(fin.Contains("MC") || pwd.Contains("MC")){
      //year+=", MC fit";
      year+=", PYTHIA Perugia0 det. TPC Coherent";
    }
    else{
      if(IDy()>4){
        //year+=", ALICE Preliminary";
        year+=", data";
      }
      else{
        //year+=", TPC Coherent";
        //year+=", ALICE Preliminary data uncor. TPC Coherent";
        year+=", ALICE data uncor. TPC Coherent";
      }
    }

    if(fin.Contains("Beta22")){
      year+=" w/ C_{p}";
    }

    if(fin.Contains("Beta11")){
      year+=" w/o C_{p}";
    }
    
    if(fin.Contains("Xmin-9")){
      year+=" full p range";
    }

    if(fin.Contains("GSI")){
      year+=" GSI";
    }

    if(fin.Contains("rb1")){
      year+=" no rebin";
    }

    if(fin.Contains("new")){
      year+=" new results";
    }

    if(fin.Contains("oldtest")){
      year+=" old test";
    }
  }
  else if(fin.Contains("10d")){
    year = "10d";
  }
  else if(fin.Contains("11a")){
    year = "11a";
  }
  else if(fin.Contains("10c")){
    year = "10c";
  }

  if(fin.Contains("Perugia-0_") || fin.Contains("Perugia0_") ){
    year = "pp #sqrt{s}=7 TeV, PYTHIA Perugia0";
  }
  else if(fin.Contains("Perugia2011_")){
    year = "pp #sqrt{s}=7 TeV, PYTHIA Perugia2011";
  }
  else if(fin.Contains("Perugia2010NoCR_")){
    year = "pp #sqrt{s}=7 TeV, PYTHIA Perugia0NoCR";
  }
  else if(fin.Contains("PerugiaZ2")){
    year = "pp #sqrt{s}=7 TeV, PYTHIA PerugiaZ2";
  }

  TString seta;
  if(fin.Contains("ETA0")){
    fkETA=0;
    seta="|#eta|<0.2";
  }
  else  if(fin.Contains("ETA1")){
    fkETA=1;
    seta="0.2<|#eta|<0.4";
  }
  else  if(fin.Contains("ETA2")){
    fkETA=2;
    seta="0.4<|#eta|<0.6";
  }
  else  if(fin.Contains("ETA3")){
    fkETA=3;
    if(fkjet){
      seta="0.6<|#eta|<0.9";
    }
    else
      seta="0.6<|#eta|<0.8";
  }
  else if(fin.Contains("ETAALL")){
    if(fkjet){
      seta="";//"|#eta|<0.9";
    }
    else{
      seta="|#eta|<0.8";
    }
  }
 
  TString tpcopt;

  if(fkcmpjpt || fkTheory){
    tpcopt=" ";
  }
  else{
    if(fin.Contains("TPC0") || pwd.Contains("TPC0")){
      tpcopt="TPC no cut";
    }
    else if(fin.Contains("TPC1") || pwd.Contains("TPC1")){
      tpcopt="TPC N>60";
    }
    else if(fin.Contains("TPC2") || pwd.Contains("TPC2")){
      tpcopt="TPC Geo";
    }
  }

  TString tofopt;
  if(fkcmpjpt==0 && fkTheory==0){
  if(fin.Contains("TOF0")){// || pwd.Contains("TOF0")){
    tofopt += " no PreID";
  }
  else if(fin.Contains("TOF1")){// || pwd.Contains("TOF1")){
    tofopt += " TPC 5][5 TOF 2][2";
  }
  else if(fin.Contains("TOF2")){// || pwd.Contains("TOF2")){
    tofopt += " TPC 5][5 TOF 2][3";
  }
  else if(fin.Contains("TOF3")){// || pwd.Contains("TOF3")){
    tofopt += " TPC 5][5 TOF 3][3";
  }
  else if(fin.Contains("TOF4")){// || pwd.Contains("TOF4")){
    tofopt += " TPC 5][5 TOF 3][4";
  }
  else if(fin.Contains("TOF9")){// || pwd.Contains("TOF9")){
    tofopt += " TPC 5][5";
  }
  }

  TString kin;
  TString ktag(fin+pwd);
  ktag.ReplaceAll("_",".");
  if(ktag.Contains("5-100.")){
    kin="#it{p}_{T@jet}^{ch} 5-100 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-100.")){
    kin="#it{p}_{T@jet}^{ch} 10-100 GeV/#it{c}" ;
  }
  else if(ktag.Contains("5-10.")){
    kin="#it{p}_{T@jet}^{ch} 5-10 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-15.")){
    kin="#it{p}_{T@jet}^{ch} 10-15 GeV/#it{c}" ;
  }
  else if(ktag.Contains("15-20.")){
    kin="#it{p}_{T@jet}^{ch} 15-20 GeV/#it{c}" ;
  }

  else if(ktag.Contains("5-6.")){
    kin="#it{p}_{T@jet}^{ch} 5-6 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-11.")){
    kin="#it{p}_{T@jet}^{ch} 10-11 GeV/#it{c}" ;
  }
  else if(ktag.Contains("15-16.")){
    kin="#it{p}_{T@jet}^{ch} 15-16 GeV/#it{c}" ;
  }

  else if(ktag.Contains("5-7.")){
    kin="#it{p}_{T@jet}^{ch} 5-7 GeV/#it{c}" ;
  }
  else if(ktag.Contains("7-10.")){
    kin="#it{p}_{T@jet}^{ch} 7-10 GeV/#it{c}" ;
  }
  else if(ktag.Contains("5-9.")){
    kin="#it{p}_{T@jet}^{ch} 5-9 GeV/#it{c}" ;
  }
  else if(ktag.Contains("9-15.")){
    kin="#it{p}_{T@jet}^{ch} 9-15 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-20.")){
    kin="#it{p}_{T@jet}^{ch} 10-20 GeV/#it{c}" ;
  }

  else if(ktag.Contains("5-6.")){
    kin="#it{p}_{T@jet}^{ch} 5-6 GeV/#it{c}" ;
  }
  else if(ktag.Contains("6-7.")){
    kin="#it{p}_{T@jet}^{ch} 6-7 GeV/#it{c}" ;
  } 
  else if(ktag.Contains("7-8.")){
    kin="#it{p}_{T@jet}^{ch} 7-8 GeV/#it{c}" ;
  }
  else if(ktag.Contains("8-9.")){
    kin="#it{p}_{T@jet}^{ch} 8-9 GeV/#it{c}" ;
  }
  else if(ktag.Contains("9-10.")){
    kin="#it{p}_{T@jet}^{ch} 9-10 GeV/#it{c}" ;
  }

  else if(ktag.Contains("10-16.")){
    kin="#it{p}_{T@jet}^{ch} 10-16 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-17.")){
    kin="#it{p}_{T@jet}^{ch} 10-17 GeV/#it{c}" ;
  }

  else if(ktag.Contains("20-100.")){
    kin="#it{p}_{T@jet}^{ch} 20-100 GeV/#it{c}" ;
  }
  else if(ktag.Contains("20-30.")){
    kin="#it{p}_{T@jet}^{ch} 20-30 GeV/#it{c}" ;
  }
  else if(ktag.Contains("10-40.")){
    kin="#it{p}_{T@jet}^{ch} 10-40 GeV/#it{c}" ;
  }

  if(ktag.Contains("Incl") || ktag.Contains("incl")){
    kin=Form("inclusive (d#it{N}/d#it{y})_{%s}/(d#it{N}/d#it{y})_{#pi}#times#Delta#it{y}_{%s}/#Delta#it{y}_{#pi}",types[ipar].Data(),types[ipar].Data());
    //kin=Form("inclusive #frac{(d#it{N}/d#it{y})_{%s}}{(d#it{N}/d#it{y})_{#pi}}#times#frac{#Delta#it{y}_{%s}}{#Delta#it{y}_{#pi}}",types[ipar].Data(),types[ipar].Data());
  }

  if(ktag.Contains("x-check")){
    kin="x-check";
  }

  TString identifier;
  const TString ids[]={spar, year, kin, seta, tpcopt, tofopt};
  for(Int_t ii=0; ii<(Int_t)(sizeof(ids)/sizeof(TString)); ii++){
    TString tmpid = ids[ii];

    while(tmpid.Contains("  ")){
      tmpid.ReplaceAll("  "," ");
    }

    if(tmpid.Length()==0)
      continue;

    identifier+=ids[ii];
    identifier+=",";
    printf("%d GetGr ids: %s ; identifier: %s\n", ii, ids[ii].Data(), identifier.Data());
  }
  //identifier.Replace(identifier.Length()-1,1,"");
  printf("%s GetGr identifier: %s\n\n", fin.Data(), identifier.Data());

  for(Int_t itype=0; itype<4; itype++){
    if(itype!=ipar)
      continue;

    TGraphAsymmErrors * tmp = 0x0;

    tmp=(TGraphAsymmErrors*) ff->Get(StatName(itype));
    if(!tmp){
      printf("no grstat! %d\n", itype); ff->ls(); exit(1);
    }
    XGLUtils::PrintGr(ff->GetName(), tmp);

    TGraphAsymmErrors * grstat=(TGraphAsymmErrors * ) tmp->Clone(Form("%sEta%d", tmp->GetName(), fkETA)); ll->Add(grstat);
    grstat->SetTitle(Form("%s %s",grstat->GetTitle(), seta.Data()));
    //grstat->SetName(Form("%sEta%d",grstat->GetName(), fkETA));  

    tmp = 0x0;
    tmp = (TGraphAsymmErrors*) ff->Get(SysName(itype));
    if(!tmp){
      printf("no grsys! %d\n", itype); ff->ls(); exit(1);
    }
    XGLUtils::PrintGr(ff->GetName(), tmp);

    TGraphAsymmErrors * grsys =(TGraphAsymmErrors*) tmp->Clone(Form("%sEta%d", tmp->GetName(), fkETA)); ll->Add(grsys);  
    grsys->SetTitle(Form("%s %s",grsys->GetTitle(), seta.Data()));
    //grsys->SetName(Form("%sEta%d",grsys->GetName(), fkETA));  

    if(kNoErr){
      for(Int_t itmppt=0; itmppt<grstat->GetN(); itmppt++){
        grstat->SetPointEYlow(itmppt,0);
        grstat->SetPointEYhigh(itmppt,0);
      }
      for(Int_t itmppt=0; itmppt<grsys->GetN(); itmppt++){
        grsys->SetPointEYlow(itmppt,0);
        grsys->SetPointEYhigh(itmppt,0);
      }
    }
    
    if(IDy()==1 || IDy()==2){
      const Double_t dlogx = TMath::Log10(grstat->GetX()[1]/grstat->GetX()[0])/2.;
      for(Int_t itmppt=0; itmppt<grstat->GetN(); itmppt++){
        const Double_t xx = grstat->GetX()[itmppt];        
        grstat->SetPointEXlow(itmppt, xx - pow(10, log10(xx)-dlogx));
        grstat->SetPointEXhigh(itmppt,pow(10, log10(xx)+dlogx ) - xx);
      }
      for(Int_t itmppt=0; itmppt<grsys->GetN(); itmppt++){
        const Double_t xx = grsys->GetX()[itmppt];        
        grsys->SetPointEXlow(itmppt, xx - pow(10, log10(xx)-dlogx));
        grsys->SetPointEXhigh(itmppt,pow(10, log10(xx)+dlogx ) - xx);
      }
    }
    /*
      if(fkjet || (fphy.Contains("2PI")&& itype==2)){
      for(Int_t itmppt=0; itmppt<grsys->GetN(); itmppt++){
      const Double_t tmptmppt = grsys->GetX()[itmppt];
      const Double_t pt0 = 0;
      const Double_t pt1 = 3;
      if(tmptmppt > pt0 && tmptmppt < pt1 && grsys->GetEYlow()[itmppt]>EPSILON){
      printf("blowing up %d %e -- %e %e %e\n", itmppt, tmptmppt, grstat->GetEYlow()[itmppt], grsys->GetEYlow()[itmppt], grsys->GetY()[itmppt]);
      grsys->GetEYhigh()[itmppt] += 4*grstat->GetEYlow()[itmppt]*TMath::Sin((tmptmppt-pt0)/(pt1-pt0)*TMath::Pi()); 
      grsys->GetEYlow()[itmppt]=TMath::Min(grsys->GetEYhigh()[itmppt],grsys->GetY()[itmppt]);
      }
      }
      }
    */

    grstat->GetXaxis()->SetTitle("#it{p}_{T}^{track} (GeV/#it{c})");
    if(IDx()==0){
      grstat->GetXaxis()->SetTitle("p (GeV/#it{c})");
    }
    else if(IDx()==2){
      //grstat->GetXaxis()->SetTitle(Form("#it{z}^{%s}",types[itype].Data()));
      grstat->GetXaxis()->SetTitle("#it{z}^{ch}");
    }
    else if(IDx()==3){
      grstat->GetXaxis()->SetTitle("#xi^{ch} = ln (jet_{#it{p}_{T}} / track_{#it{p}_{T}})");
    }

    if(IDy()==0){
      grstat->GetYaxis()->SetTitle(year.Contains("Perugia")? "fraction" : Form("uncorrected %s fraction", types[itype].Data()));
    }
    else if(IDy()==3){
      //grstat->GetYaxis()->SetTitle(Form("uncorrected (%s)/(%s)",types[itype].Data(),types[1].Data()));
      grstat->GetYaxis()->SetTitle(Form("uncorrected %s/%s",types[itype].Data(),types[1].Data()));
    }
    else if(IDy()==8){
      grstat->GetYaxis()->SetTitle(Form("TOF PID efficiency %s",types[itype].Data()));
    }    
    else if(IDy()==1){
      grstat->GetYaxis()->SetTitle(Form("%s dE/dx mean",types[itype].Data()));
    }    
    else if(IDy()==2){
      grstat->GetYaxis()->SetTitle(Form("%s dE/dx resolution",types[itype].Data()));
    }    
    else if(IDy()==5){
      if(IDx()==1){
        //grstat->GetYaxis()->SetTitle(Form("unc. 1/#it{N}_{jets} d#it{N}^{%s}/d#it{p}_{T}^{track}^{%s} (GeV/#it{c})^{-1}", types[itype].Data(),types[itype].Data()));
        grstat->GetYaxis()->SetTitle(Form("unc. 1/#it{N}_{jets} d#it{N}^{%s}/d#it{p}_{T}^{track} (GeV/#it{c})^{-1}", types[itype].Data()));
      }
      else if(IDx()==2){
        //grstat->GetYaxis()->SetTitle(Form("unc. 1/#it{N}_{jets} d#it{N}^{%s}/d#it{z}^{%s}", types[itype].Data(),types[itype].Data()));
        grstat->GetYaxis()->SetTitle(Form("unc. 1/N_{jets} d#it{N}^{%s}/d#it{z}^{ch}", types[itype].Data()));
      }
      else if(IDx()==3){
        grstat->GetYaxis()->SetTitle(Form("unc. 1/#it{N}_{jets} d#it{N}^{%s}/#xi^{%s}", types[itype].Data(),types[itype].Data()));
      }
    }
    else if(IDy()==6){
      if(IDx()==1){
        grstat->GetYaxis()->SetTitle(Form("1/#it{N}_{jets} d#it{N}^{%s}/d#it{p}_{T}^{track} (GeV/#it{c})^{-1}", types[itype].Data()));
      }
      else if(IDx()==2){
        //grstat->GetYaxis()->SetTitle(Form("1/#it{N}_{jets} d#it{N}^{%s}/d#it{z}^{%s}", types[itype].Data(),types[itype].Data()));
        grstat->GetYaxis()->SetTitle(Form("1/#it{N}_{jets} d#it{N}^{%s}/d#it{z}^{ch}", types[itype].Data()));
      }
      else if(IDx()==3){
        grstat->GetYaxis()->SetTitle(Form("1/#it{N}_{jets} d#it{N}^{%s}/#xi^{%s}", types[itype].Data(),types[itype].Data()));
      }
    }
    else if(IDy()==7){
      grstat->GetYaxis()->SetTitle(Form("%s/%s",types[itype].Data(),types[1].Data()));
    }

    if(fkjet){
      
      const Double_t tmpsc = fklogx?1.5:1.1;
      Double_t tmpxmax = grstat->GetXaxis()->GetXmax()/tmpsc;
      if(kin.Contains("-10 "))
        tmpxmax = 10;
      else if(kin.Contains("-15 "))
        tmpxmax = 15;
      else if(kin.Contains("-20 "))
        tmpxmax = 20;
      printf("=============================== tmpxmax %e\n", tmpxmax);

      tmpxmax*= tmpsc;
      
      //SetLimits important for SetMinimum to take effect!!
      if(IDx()==1){
        //grstat->GetXaxis()->SetLimits(fklogx?0.4:0, tmpxmax+(fklogx?1:1));//20.2);
        //setlimits has to call values other than Xmin and Xmax so setminimum is activated!
        grstat->GetXaxis()->SetLimits(fklogx?0.1:0, tmpxmax);
      }
      else if(IDx()==2){
        grstat->GetXaxis()->SetLimits(-0.01,1.01);
      }
      else if(IDx()==3){
        grstat->GetXaxis()->SetLimits(-0.05, grstat->GetXaxis()->GetXmax());//2.15
      }
      else{
        grstat->GetXaxis()->SetLimits(fklogx?0.1:0, tmpxmax+(fklogx?10:1));
      }

      if(IDy()==8){
        //grstat->GetXaxis()->SetLimits(0.1, 5);//grstat->GetXaxis()->GetXmin(), grstat->GetXaxis()->GetXmax());
        //to avoid TPC 5][5
        grstat->GetXaxis()->SetLimits(0.7, 5);//grstat->GetXaxis()->GetXmin(), grstat->GetXaxis()->GetXmax());
      }

      if( (IDy()==6 || IDy()==7) && IDx()==1){
        if(itype==1){
          if(year.Contains("Perugia")){
            printf("testtestperugiapion\n");
            Double_t tmpx[grstat->GetN()];
            Double_t tmpy[grstat->GetN()];
            Int_t tmpn = 0;
            for(Int_t iold=0; iold<grstat->GetN(); iold++){
              if(grstat->GetX()[iold]>0.15){// && grstat->GetX()[iold]<tmpxmax){                                                                                          
                tmpx[tmpn] = grstat->GetX()[iold];
                tmpy[tmpn] = grstat->GetY()[iold];
                tmpn++;
              }
            }
            for(Int_t inew=0; inew<tmpn; inew++){
              grstat->SetPoint(inew, tmpx[inew], tmpy[inew]);
              grstat->SetPointError(inew, 0,0,0,0);
            }
            grstat->Set(tmpn);
          }
          else{            
            grstat->GetXaxis()->SetLimits(0.14, 22);
          }

        }
        else{
          //grstat->GetXaxis()->SetLimits(0.34, 22);
          if(year.Contains("Perugia")){
            printf("testtestperugia\n");
            //grstat->GetXaxis()->SetLimits(0.35, tmpxmax);
            //grstat->Draw("al");
            //grstat->GetXaxis()->SetRangeUser(0.35, tmpxmax);

            Double_t tmpx[grstat->GetN()];
            Double_t tmpy[grstat->GetN()];
            Int_t tmpn = 0;
            for(Int_t iold=0; iold<grstat->GetN(); iold++){
              if(grstat->GetX()[iold]>0.35){// && grstat->GetX()[iold]<tmpxmax){
                tmpx[tmpn] = grstat->GetX()[iold];
                tmpy[tmpn] = grstat->GetY()[iold];
                tmpn++;
              }
            }
            for(Int_t inew=0; inew<tmpn; inew++){
              grstat->SetPoint(inew, tmpx[inew], tmpy[inew]);
              grstat->SetPointError(inew, 0,0,0,0);
            }
            grstat->Set(tmpn);
          }
          else{
            grstat->GetXaxis()->SetLimits(0.24, 22);
          }
        }
      }
      if( (IDy()==6 || IDy()==7) && IDx()==2){
        if(year.Contains("Perugia")){
          if( (itype==0 && kin.Contains("5-10")) || (itype==2 && (kin.Contains("5-10") || kin.Contains("10-15"))) ){
            printf("testtestZperugia\n");
            //grstat->GetXaxis()->SetLimits(0.35, tmpxmax);
            //grstat->Draw("al");
            //grstat->GetXaxis()->SetRangeUser(0.35, tmpxmax);
            
            Double_t tmpx[grstat->GetN()];
            Double_t tmpy[grstat->GetN()];
            Int_t tmpn = 0;
            for(Int_t iold=0; iold<grstat->GetN(); iold++){
            if(grstat->GetX()[iold]>0.04){// && grstat->GetX()[iold]<tmpxmax){
              tmpx[tmpn] = grstat->GetX()[iold];
              tmpy[tmpn] = grstat->GetY()[iold];
              tmpn++;
            }
            }
            for(Int_t inew=0; inew<tmpn; inew++){
              grstat->SetPoint(inew, tmpx[inew], tmpy[inew]);
              grstat->SetPointError(inew, 0,0,0,0);
            }
            grstat->Set(tmpn);
          }
        }
      }

      if(IDy()==0){
        if(itype==0){
          grstat->SetMinimum(-0.01);
          grstat->SetMaximum(0.36);
        }
        else if(itype==1){
          grstat->SetMinimum(0.45);//0.31);
          grstat->SetMaximum(1.23);//1.39);
        }
        else if(itype==2){
          grstat->SetMinimum(-0.04);
          grstat->SetMaximum(0.81);
        }
        else{
          grstat->SetMinimum(-0.04);
          grstat->SetMaximum(1.54);
        }
      }
      else if(IDy()==3 || IDy()==7 ){
        if(itype==0){
          grstat->SetMinimum(-0.02);
          grstat->SetMaximum(0.56);//1.09);//
        }
        else if(itype==2){
          grstat->SetMinimum(-0.04);
          grstat->SetMaximum(0.87);//1.37);
        }
        else{
          if(!fklogy){
            grstat->SetMinimum(-0.04);
            grstat->SetMaximum(1.54);
          }
        }
      }
      else if(IDy()==5){
        if(IDx()==1){
          grstat->SetMinimum(5e-9);
          grstat->SetMaximum(8);
          if(itype==1){
            grstat->SetMinimum(5e-5);
            grstat->SetMaximum(8);
          }
          else if(itype==3){
            grstat->SetMinimum(8e-7);
            grstat->SetMaximum(0.8);
          }
          else{
            grstat->SetMinimum(5e-10);
            grstat->SetMaximum(0.5);
          }
        }
        else{
          if(itype==1){
            grstat->SetMinimum(8e-2);
            grstat->SetMaximum(40);
          }
          else if(itype==3){
            grstat->SetMinimum(8e-4);
            grstat->SetMaximum(8);
          }
          else{
            grstat->SetMinimum(2e-3);
            grstat->SetMaximum(5);
          }
        }
      }      
      else if(IDy()==6){
        if(fklogy){
          if(IDx()==1){
            grstat->SetMinimum(3e-6);
            grstat->SetMaximum(9);
            /*
            if(itype==1){
              grstat->SetMinimum(5e-5);
              grstat->SetMaximum(8);
            }
            else if(itype==3){
              grstat->SetMinimum(8e-7);
              grstat->SetMaximum(0.8);
            }
            else{
              grstat->SetMinimum(5e-6);
              grstat->SetMaximum(0.5);
            }
            */
          }
          else{
            grstat->SetMinimum(2e-4);
            grstat->SetMaximum(50);
            /*
            if(itype==1){
              grstat->SetMinimum(5e-2);
              grstat->SetMaximum(40);
            }
            else if(itype==3){
              grstat->SetMinimum(8e-4);
              grstat->SetMaximum(8);
            }
            else{
              grstat->SetMinimum(2e-3);
              grstat->SetMaximum(5);
            }
            */
          }
        }
        else{
          if(IDx()==1){
            if(itype==0){
              grstat->SetMinimum(-0.01);
              grstat->SetMaximum(0.13);
            }
            else if(itype==1){
              grstat->SetMinimum(-0.2);
              grstat->SetMaximum(2.8);
            }
            else if(itype==3){
              grstat->SetMinimum(8e-7);
              grstat->SetMaximum(0.8);
            }
            else{
              grstat->SetMinimum(-0.01);
              grstat->SetMaximum(0.33);
            }
          }
          else{
            if(itype==0){
              grstat->SetMinimum(-0.1);
              grstat->SetMaximum(2);
            }
            else if(itype==1){
              grstat->SetMinimum(-2);
              grstat->SetMaximum(30);
            }
            else if(itype==3){
              grstat->SetMinimum(8e-4);
              grstat->SetMaximum(8);
            }
            else{
              grstat->SetMinimum(-0.2);
              grstat->SetMaximum(3.5);
            }
          }
        }
      }
      else if(IDy()==8){
        grstat->SetMinimum(-0.03);
        grstat->SetMaximum(1.3);
      }
      else if(IDy()==1){
        grstat->SetMinimum(35);
        grstat->SetMaximum(150);
        if(itype==3){
          grstat->SetMinimum(71.5);
          grstat->SetMaximum(80.5);
        }
      }
      else if(IDy()==2){
        grstat->SetMinimum(0.01);
        grstat->SetMaximum(0.14);
      }

      if(fklogy && (IDy()==0 || IDy()==3 ) && (itype==0 || itype==2)){
        grstat->SetMinimum(5e-9);
        grstat->SetMaximum(1.07);

        if(year.Contains("MC")){
          grstat->SetMinimum(2e-5);
          grstat->SetMaximum(0.8);
        }


        if(IDx()==2){
          grstat->SetMinimum(5e-4);
          grstat->SetMaximum(0.8);
        }

        if(IDx()==0||IDx()==1){
          grstat->GetXaxis()->SetLimits(0.1,10);
        }
      }
    }

    printf("grstat max %f min %f\n", grstat->GetMaximum(), grstat->GetMinimum());
  }

  ff->Close();
  delete ff;

  return identifier;
}

void drawGr(TPad * pad0, TPad * pad1, const Int_t drawid, const Int_t refid, TList * ll, const TString ytit,  TString sid, const Int_t headerbit, TLegend *lg, const Bool_t kfirst, const Int_t col, const Int_t msty, const Int_t lsty)
{
  printf("\n check refid %d\n", refid);

  style::fgkTitleSize = 0.06;
  style::fgkTextSize = 0.07;
  style::fgkYTitleOffset = 1.1;//0.9;
  style::PadSetup(pad0);
  pad0->SetLeftMargin(0.14);
  pad0->SetRightMargin(0.03);
  pad0->SetTopMargin(0.025);
  pad0->SetBottomMargin(0.01);
  pad0->cd();

  //------------

  TString tokens[]={"","","","","","","","","","","",""};
  Int_t itoken=0;
  while(sid.Contains(",")&&sid.Length()>1){
    tokens[itoken]=sid(0, sid.First(","));
    sid=sid(sid.First(",")+1, sid.Length());
    //printf("%s %s\n", tokens[itoken].Data(), sid.Data());
    itoken++;
  }
  TString lhea;
  TString lent;
  for(Int_t ii=0; ii<itoken; ii++){
    if( (1<<ii)&headerbit ){
      TString tmptk = tokens[ii];

      /*
      if((tmptk.Contains("+#")||tmptk.Contains("+K")||tmptk.Contains("+e")) && tmptk.Contains("/")){
        if(!tmptk.Contains("GeV")){
          tmptk.Prepend("(");
          tmptk.Append(")");
        }
        tmptk.ReplaceAll("/",")/(");
        tmptk.ReplaceAll("GeV)/(c","GeV/#it{c}");
      }
      */

      lhea+=tmptk;
      lhea+=" ";
    }
    else{
      lent+=tokens[ii];
      lent+=" ";
    }
  }
  printf("%d %d %s header %s entry %s\n", drawid, refid, sid.Data(), lhea.Data(), lent.Data());

  style::ResetStyle(lg, lgmar);

  TString etatag;
  if(gETA>=0 && !lhea.Contains("eta") ){
    if(gETA==0){
      etatag=" |#eta|<0.2";
    }
    else if(gETA==1){
      etatag=" 0.2<|#eta|<0.4";
    }
    else if(gETA==2){
      etatag=" 0.4<|#eta|<0.6";
    }
    else{
      printf("bad gETA %d\n", gETA); exit(1);
    }
  }

  TString hhd(lhea+etatag);
  hhd.ReplaceAll("  "," ");
  while(hhd[0]==' '){
    hhd=hhd(1,hhd.Length()-1);
  }

  
  const TString jeth1("#splitline{#it{p}_{T}^{track}>0.150 GeV/#it{c}}{|#eta^{track}|<0.9}");
  const TString jeth0("#splitline{FastJet anti-#it{k}_{T}}{ #it{R}=0.4; |#eta^{jet}|<0.5}");

  //if(fkcmpjpt || fkTheory){
  if(0){
    TLatex * lt0 = 0x0;
    TLatex * lt1 = 0x0;

    if(fkpar==2 && fgrid==16){
      lt0=new TLatex(0.74,fkcmpjpt?0.51:0.43, jeth0);
      lt1=new TLatex(0.74,fkcmpjpt?0.34:0.26, jeth1);
    }
    else{
      lt0=new TLatex(0.2,0.14, jeth0);
      lt1=new TLatex(0.2,0.09, jeth1);
    }
    //lt0->SetTextAlign(33);
    //lt1->SetTextAlign(33);
    style::ResetStyle(lt0,0.8);
    lt0->Draw();
    style::ResetStyle(lt1,0.8);
    lt1->Draw();
  }
  
  Double_t latyadd = 0.55*(fkpar==0);
  if(IDy()==6){
    latyadd = 0.65;
  }

  Double_t latx1 = 0.73;
  Double_t latx2 = 0.61;

  TString sfill = "        ";

  if(IDy()==6 && IDx()==1 && fkpar==1){
    latx1 =0.18;
    latx2=0.18;
    sfill="";
    latyadd -= 0.15;
  }

  TLatex * ltpre1 = new TLatex(latx1, 0.245+latyadd, "ALICE Preliminary");
  TLatex * ltpre2 = new TLatex(latx2, 0.135+latyadd, Form("#splitline{%santi-#it{k}_{T}; #it{R}=0.4; |#eta^{jet}|<0.5}{#it{p}_{T}^{track}>0.15 GeV/#it{c}; |#eta^{track}|<0.9}", sfill.Data()));
  style::ResetStyle(ltpre1,0.8);
  style::ResetStyle(ltpre2,0.8);
  ltpre1->Draw();
  ltpre2->Draw();

  //remove 
  hhd.ReplaceAll(" TPC 5][5","");
  hhd.ReplaceAll(" no PreID","");
  hhd.ReplaceAll(" TPC N>60","");
  hhd.ReplaceAll("@",",");
  hhd.ReplaceAll("V data","V");
  lg->SetHeader(hhd);

  //---------------------------------------
 
  TGraphAsymmErrors * grStat=(TGraphAsymmErrors *) ((TGraphAsymmErrors *) ll->At(drawid*2))->Clone(Form("stat%d",drawid)); if(glist) glist->Add(grStat);
  TGraphAsymmErrors * grSys =(TGraphAsymmErrors *) ((TGraphAsymmErrors *) ll->At(drawid*2+1))->Clone(Form("sys%d",drawid)); if(glist) glist->Add(grSys);

  printf("=======>> test drawGr grstat max %f min %f xtit %s ytit %s\n", grStat->GetMaximum(), grStat->GetMinimum(), grStat->GetXaxis()->GetTitle(), grStat->GetYaxis()->GetTitle());

  style::ResetStyle(grStat);
  style::ResetStyle(grSys);
  //grStat->SetMarkerColor(col==kGray+2?kBlack:col);
  //grStat->SetLineColor(col==kGray+2?kBlack:col);
  grStat->SetMarkerColor(col);
  grStat->SetLineColor(col);
  grStat->SetMarkerStyle(msty);
  grStat->SetLineStyle(lsty);
  grStat->SetFillStyle(fsty[drawid]);
  grStat->SetFillColor(col);

  //grSys->SetMarkerColor(col==kGray+2?kBlack:col);
  //grSys->SetLineColor(col==kGray+2?kBlack:col);
  grSys->SetMarkerColor(col);
  grSys->SetLineColor(col);
  grSys->SetMarkerStyle(msty);
  grSys->SetLineStyle(lsty);
  grSys->SetFillStyle(fsty[drawid]);
  grSys->SetFillColor(col);

  //=========>

  TGraphAsymmErrors * grRef = (TGraphAsymmErrors *) (TGraphAsymmErrors *) ll->At(refid*2);

  TGraphAsymmErrors * outStat = 0x0;
  TGraphAsymmErrors * outSys = 0x0;

  //style::ScaleToRef(grStat, grSys, grRef, kFALSE, outStat, outSys); if(glist) glist->Add(outStat); if(glist) glist->Add(outSys);
  style::SimpleScaleToRef(grStat, grSys, grRef, outStat, outSys); if(glist) glist->Add(outStat); if(glist) glist->Add(outSys);

  //<=====================


  if(lent.Contains("Perugia")){
    if(lent.Contains("Perugia0NoCR")){
      grStat->SetLineStyle(kDotted);
    }
    else if(lent.Contains("Perugia0")){
      grStat->SetLineStyle(kSolid);
    }
    else if(lent.Contains("Perugia2011")){
      grStat->SetLineStyle(kDashed);
    }
    grStat->SetLineWidth(2);
    grStat->Set(outStat->GetN());

    grStat->Draw(kfirst?"alX":"lX");
    //grSys->Draw("lX");                                                                                                                                                                            
  }
  else{
    //test grStat->Draw(kfirst?"apl":"pl");
    grStat->Draw(kfirst?"ap":"p");
    grSys->Draw(/*"3"*/ lent.Contains("ITS")?"3":"2");//"3"
    //grStat->Draw("pl");
  }

  //if(!ftag.Contains("cmpJpt")){
  lent.ReplaceAll("#it{p}_{T@jet}^{ch} ","");
  //}
  lent.ReplaceAll("@",",");
  TString lopt = lent.Contains("truth")?"lp":"lpf";
  if(lent.Contains("Perugia")){
    lopt = "l";
  }
  lg->AddEntry(grStat,lent+(drawid==refid?"(ref.)":""), lopt); 

  //---------------------------------------
  style::fgkTitleSize = 0.1;
  style::fgkTextSize = 0.1;
  style::fgkYTitleOffset = 0.6;
  style::fgkXTitleOffset = 1.35;
  style::PadSetup(pad1);
  pad1->SetTopMargin(0.01);
  pad1->SetLeftMargin(0.14);
  pad1->SetRightMargin(0.03);
  pad1->SetBottomMargin(0.3);
  pad1->cd();

  style::ResetStyle(outStat);
  style::ResetStyle(outSys);
  outStat->GetXaxis()->SetMoreLogLabels();
  outStat->GetYaxis()->SetNdivisions(505);

  const TString pwd=gSystem->pwd();

  if(fkpar==1){// || pwd.Contains("MCtrue")){
    outStat->SetMaximum(1.06);
    outStat->SetMinimum(0.94);
  }
  else{
    outStat->SetMaximum(1.26);
    outStat->SetMinimum(0.74);   
  }
  
  if(pwd.Contains("cmpRec")){
  //if(ftag.Contains("cmpTPC")||ftag.Contains("cmpTOF")){
    if(fkpar==1){// || pwd.Contains("MCtrue")){
      outStat->SetMaximum(1.16);
      outStat->SetMinimum(0.84);
    }
    else{
      outStat->SetMaximum(1.46);
      outStat->SetMinimum(0.54);
    }
    //}
    if(ftag.Contains("cmpJpt") && fkpar==0){
      /*
        if(fkpar==1){// || pwd.Contains("MCtrue")){
        outStat->SetMaximum(1.36);
        outStat->SetMinimum(0.64);
        }
        else{
      */
      outStat->SetMaximum(1.86);
      outStat->SetMinimum(0.14);
      //}
    }
  }

  if(IDy()==1){
    outStat->SetMaximum(1.021);
    outStat->SetMinimum(0.979);
  }
  else if(IDy()==2){
    outStat->SetMaximum(1.105);
    outStat->SetMinimum(0.895);
  }

  if(gETA>=0){
    if(IDy()==1){
      if(fkpar==3){
        outStat->SetMaximum(1.0095);
        outStat->SetMinimum(0.9905);
      }
      else{
        outStat->SetMaximum(1.0025);
        outStat->SetMinimum(0.9975);
      }
    }
    if(IDy()==2 && fkpar!=3){
      outStat->SetMaximum(1.025);
      outStat->SetMinimum(0.975);
    }
  }

  if(fklogy && (fkpar==0 || fkpar==2)){
      outStat->SetMinimum(-0.1);
      outStat->SetMaximum(2.1);
  }

  if(IDy()==5){
      outStat->SetMinimum(-0.1);
      outStat->SetMaximum(2.1);
  }
  if(IDy()==6 || IDy()==7){
    outStat->SetMinimum(-0.1);

    if(IDy()==7){
      outStat->SetMinimum(0.25);
    }

    outStat->SetMaximum(2.4);

    if(fkTheory){
      outStat->SetMinimum(0.3);
      outStat->SetMaximum(2.8);
    }

      /*
      if(fkTheory  || IDy()==7){
        if(fkpar==0){
          outStat->SetMinimum(-0.05);//0.38);
          outStat->SetMaximum(2.05);//1.62);
        }
        else if(fkpar==1){
          outStat->SetMinimum(0.45);
          outStat->SetMaximum(1.55);
        }
        else if(fkpar==2){
          outStat->SetMinimum(-0.05);//0.3);
          outStat->SetMaximum(2.05);//1.7);
        }
        else{
          outStat->SetMinimum(0.48);
          outStat->SetMaximum(1.22);
        }
      }
      */
      //if(fkcmpjpt || fgrid==16){
        /*
        gPad->SetLogy(0);
        if(fkpar==2){
          gPad->SetLogy(1);
          outStat->SetMinimum(0.45);
          outStat->SetMaximum(110);
        }
        */
      //}
  }

  outStat->GetYaxis()->SetTitle(ytit);

  //outStat->SetMarkerColor(col==kGray+2?kBlack:col);
  //outStat->SetLineColor(col==kGray+2?kBlack:col);
  outStat->SetMarkerColor(col);
  outStat->SetLineColor(col);
  outStat->SetMarkerStyle(msty);
  outStat->SetLineStyle(lsty);
  //outStat->SetFillStyle(fsty);
  //outStat->SetFillColor(col);

  //outSys->SetMarkerColor(col==kGray+2?kBlack:col);
  //outSys->SetLineColor(col==kGray+2?kBlack:col);
  outSys->SetMarkerColor(col);
  outSys->SetLineColor(col);
  outSys->SetMarkerStyle(msty);
  outSys->SetLineStyle(lsty);
  outSys->SetFillStyle(fsty[drawid]);
  outSys->SetFillColor(col);

  //test outStat->Draw(kfirst?"apl":"pl");
  if(lent.Contains("Perugia")){
    if(lent.Contains("Perugia0NoCR")){
      outStat->SetLineStyle(kDotted);
    }
    else if(lent.Contains("Perugia0")){
      outStat->SetLineStyle(kSolid);
    }
    else if(lent.Contains("Perugia2011")){
      outStat->SetLineStyle(kDashed);
    }
    outStat->SetLineWidth(2);

    outStat->Draw(kfirst?"alX":"lX");
    //outSys->Draw("lX");
  }
  else{
    outStat->Draw(kfirst?"ap":"p");
    outSys->Draw(/*"3"*/lent.Contains("ITS")?"3":"2");
  }
  //outStat->Draw("pl");

  /*
  TGraphAsymmErrors * gRevStat = style::GetInverse(outStat);
  TGraphAsymmErrors * gRevSys = style::GetInverse(outSys);

  ll->Add(gRevStat);
  ll->Add(gRevSys);
  */

}

void drawFractionRatio(const TString tag, const Int_t ipar, const TString fin0, const TString fin1="", const TString fin2="", const TString fin3="", const TString fin4="", const TString fin5="", const Bool_t rawkmean=kFALSE, const TString ytit="fit/true", const Int_t headerbit=16, const Bool_t klogx=kTRUE, const Bool_t klogy=kFALSE, const TString fin6="", const TString fin7="", const TString fin8="", const TString fin9="")
{
  //style::fgkLineWidth = 1;
  style::fgkMarkerSize = 1;

  fkTheory=tag.Contains("cmpTheo");
  fkcmpjpt=tag.Contains("cmpJpt");

  Int_t kmean = rawkmean;

  printf("position 0\n");
  ftag = tag;

  fkpar = ipar;

  const TString pwd=gSystem->pwd();
  if(pwd.Contains("POS")){
    memcpy(types, ps, sizeof(ps));
    for(Int_t ii=0; ii<4; ii++){
      outname[ii]+="plus";
    }
  }
  else if(pwd.Contains("NEG")){
    memcpy(types, ns, sizeof(ns));
    for(Int_t ii=0; ii<4; ii++){
      outname[ii]+="minus";
    }
  }
  else {
    //TString tmpss[]={"p+#bar{p}","#pi^{+}+#pi^{-}","K^{+}+K^{-}","e^{+}+e"};
    TString tmpss[]={"p","#pi","K","e"};
    //bad
    //memcpy(types, tmpss, sizeof(tmpss));
    for(Int_t ii=0; ii<4; ii++){
      types[ii]=tmpss[ii];
      if(!outname[ii].Contains("All")){
        outname[ii]+="All";
      }
    }
  }

  fklogx = klogx;
  fklogy = klogy;

  const TString idts[]={pwd, tag};
  for(Int_t ii=0; ii<2; ii++){
    TString idtag = idts[ii];
    idtag.ToUpper();
    printf("idtag test %s\n", idtag.Data());

    if(idtag.Contains("ID01")){
      fgrid = 01;
    }
    else if(idtag.Contains("ID02")){
      fgrid = 02;
    }
    else if(idtag.Contains("ID51")){
      fgrid = 51;
    }
    else if(idtag.Contains("ID52")){
      fgrid = 52;
    }
    else if(idtag.Contains("ID10")){
      fgrid = 10;
    }
    else if(idtag.Contains("ID20")){
      fgrid = 20;
    }
    else if(idtag.Contains("ID30")){
      fgrid = 30;
    }
    else if(idtag.Contains("ID13")){
      fgrid = 13;
    }
    else if(idtag.Contains("ID23")){
      fgrid = 23;
    }
    else if(idtag.Contains("ID33")){
      fgrid = 33;
    }
    else if(idtag.Contains("ID60")){
      fgrid = 60;
    }
    else if(idtag.Contains("ID70")){
      fgrid = 70;
    }
    else if(idtag.Contains("ID80")){
      fgrid = 80;
    }
    else if(idtag.Contains("ID63")){
      fgrid = 63;
    }
    else if(idtag.Contains("ID73")){
      fgrid = 73;
    }
    else if(idtag.Contains("ID83")){
      fgrid = 83;
    }
    else if(idtag.Contains("ID08")){
      fgrid=8;
    }
    else if(idtag.Contains("ID58")){
      fgrid=58;
    }
    else if(idtag.Contains("ID15")){
      fgrid=15;
    }
    else if(idtag.Contains("ID25")){
      fgrid=25;
    }
    else if(idtag.Contains("ID16")){
      fgrid=16;
    }
    else if(idtag.Contains("ID26")){
      fgrid=26;
    }
    else if(idtag.Contains("ID17")){
      fgrid=17;
    }
    else if(idtag.Contains("ID27")){
      fgrid=27;
    }
  }

  if(IDx()!= 1 && IDx()!= 2 && IDx()!= 3 && IDy()!= 0 && IDy()!= 3 && IDy()!= 8 && IDy()!=1 && IDy()!=2 && IDy()!=5 && IDy()!=6 && IDy()!=7){  
    printf("bad grid %d %d %d\n", fgrid, IDx(), IDy()); exit(1);
  }

  if(IDx()!=1 && IDx()!=0 && fklogx){
    printf("bad fklogx! %d %d\n", fgrid, fklogx); exit(1);
  }  

  if(IDy()==1 || IDy()==2){
    kmean = 1;
    fklogy = 0;
  }

  //printf("testtest gETA!! %d\n", gETA);

  printf("fgrid %d %d %d\n", fgrid, IDx(), IDy());
  fkjet = pwd.Contains("FracFunc")?1:0;

  printf("position 1\n");

  TString tmpfs[]={fin0, fin1, fin2, fin3, fin4, fin5, fin6, fin7, fin8, fin9};
  const Int_t tmpcol[]={kmean?kMagenta:kGray+2, kRed, fColBl, kGreen+3, kmean?kBlack:kMagenta, kOrange+1, kOrange+2, kMagenta, kBlack, kRed};
  const Int_t tmpmsty[]={27,24,30,20,29, 21, 24, 25,25,25,21, 21,20,25,24, 25,25,25,21,21,20,25,24, 25,25,25,21};
  const Int_t tmplsty[]={kSolid, kSolid, kSolid, kSolid, kSolid, kSolid, kSolid, kSolid, kSolid, kSolid, kDashed, kSolid, kDashDotted, kSolid, kSolid, kSolid, kSolid};
  Int_t ntmp = sizeof(tmpfs)/sizeof(TString);

  Int_t tmpnf = 0;
  TString fins[100];
  for(Int_t ii=0; ii<100; ii++) fins[ii]="";
  Int_t col[100];
  Int_t msty[100];
  Int_t lsty[100];
  for(Int_t ii=0; ii<ntmp; ii++){
    tmpfs[ii].ReplaceAll(" ","");
    if(tmpfs[ii].Length()==0)
      continue;

    if(!TFile::Open(tmpfs[ii]))
      continue;

    fins[tmpnf]=tmpfs[ii];
    col[tmpnf] = tmpcol[ii+(!fkTheory)];
    if(IDy()==6 && tag.Contains("cmpJptT") && 3-ii>=0){
      col[tmpnf] = tmpcol[3-ii];
    }
    msty[tmpnf] = tmpmsty[ii];
    lsty[tmpnf] = tmplsty[ii];

    tmpnf++;
  }
  const Int_t nfile = tmpnf;
  //requiring at least 2 files
  if(nfile<2){
    if(nfile==1 && pwd.Contains("cmpRec")){
    }
    else{
      printf("nfile<2 %d\n", nfile); exit(1);
    }
  }

  //==============================================
  style::SetGlobalStyle(0);
  TCanvas *cc=new TCanvas("cc","",600,500);  //if(glist) glist->Add(cc);

  TPad *c0=new TPad("c0","",0,0.4,1,1);  //if(glist) glist->Add(c0);
  c0->SetLogx(fklogx);
  c0->SetLogy(fklogy);
  c0->Draw();

  TPad *c1= new TPad("c1","", 0,0,1,0.4); //if(glist) glist->Add(c1);
  c1->SetLogx(klogx);  
  c1->Draw();

  //------------------------------------------

  printf("position 2\n");

  TList * ll=new TList;
  ll->SetOwner(kTRUE);

  TString idens[nfile]; for(Int_t ii=0; ii<nfile; ii++) idens[ii]="";

  for(Int_t ii=0; ii<nfile; ii++){
    if(fins[ii].Length()){
      idens[ii] = GetGr(ipar, fins[ii], ll);
      printf("identifier %d: %s\n\n", ii, idens[ii].Data());
    }
  }

  printf("position 3 GetGr done! \n");

  if(kmean){
    style::SimpleCalcMean(ll, kFALSE);
  }

  ll->ls();

  printf("position 4 SimpleCalcMean done!\n");
  //----------------------------------------
  
  TLegend *lg=0x0;
  if(fkjet){
    if(fklogy && (fkpar==0||fkpar==2)){
      if(IDx()==3){
        lg = new TLegend(0.15, 0.05, 0.6, 0.5);
      }
      else{
        lg = new TLegend(0.6, 0.05, 0.95, 0.5);
        lg->SetTextAlign(33);
      }
    }
    else if(IDy()==3){
      lg = new TLegend(0.16, 0.5, 0.95, 0.95);
    }
    else{
      //lg = new TLegend(0.35, 0.5, 0.90, 0.95);
      lg = new TLegend(0.6, 0.5, 0.95, 0.95);
      lg->SetTextAlign(33);
    }
    if(IDy()==5 && IDx()==1){
      lg = new TLegend(0.3, 0.05, 0.75, 0.5);
    }
    if(IDy()==5 && IDx()==2){
      lg = new TLegend(0.2, 0.05, 0.75, 0.5);
    }
    const Double_t tmplgx0 = fkcmpjpt?0.7:0.62;
    const Double_t tmplgx1 = 0.95;
    const Double_t tmplgy0 = fkcmpjpt?0.59:0.5;
    const Double_t tmplgy1 = 0.95;
    if(IDy()==6){// && IDx()==1){
      if(fklogy){
        lg = new TLegend(0.17, 0.03, 0.75, 0.45);
      }
      else{
        //lg = new TLegend(0.65, tmplgy0, 0.88, tmplgy1);
        lg = new TLegend(tmplgx0, tmplgy0, tmplgx1,tmplgy1);
        lg->SetTextAlign(33);
      }
    }
    /*
    if(IDy()==6 && IDx()==2){
      if(klogy){
        if(fkpar==0){
          //lg = new TLegend(0.65, tmplgy0, 0.88, tmplgy1);
          lg = new TLegend(tmplgx0, tmplgy0, tmplgx1,tmplgy1);
          lg->SetTextAlign(33);
        }
        else{
          lg = new TLegend(0.2, 0.05, 0.75, 0.45);
        }
      }
      else{
        //lg = new TLegend(0.65, tmplgy0, 0.88, tmplgy1);
        lg = new TLegend(tmplgx0, tmplgy0, tmplgx1,tmplgy1);
        lg->SetTextAlign(33);
      }
    }
    */
    if(IDy()==7){// && IDx()==2){
      //lg = new TLegend(0.65, tmplgy0, 0.88, tmplgy1);
      //lg = new TLegend(0.15, tmplgy0, 0.5,tmplgy1);
      lg = new TLegend(0.16, 0.5, 0.95, 0.95);
      //lg->SetTextAlign(33);
    }
  }
  else{
    lg = new TLegend(0.6, 0.6, 0.98, 0.95);
  }
  style::ResetStyle(lg, lgmar);
 
  if(glist) glist->Add(lg);

  //MC - noreg. - pm1 - pm2
  const Int_t ncol = sizeof(col)/sizeof(Int_t);
  if(ncol<nfile){
    printf("ncol<nfile %d %d\n", ncol, nfile);exit(1);
  }

  const Int_t idmean = (ll->GetSize()/2)-1; 

  printf("position 5\n");
 
  
  for(Int_t ii=0; ii<nfile; ii++){
    if(fins[ii].Length()){
      drawGr(c0, c1, ii, kmean? idmean :0, ll, ytit, idens[ii], headerbit, lg, ii==0 /*(ii==0&&!kmean)*/, col[ii],msty[ii], lsty[ii]);
      //drawGr(c0, c1, ii, kmean? idmean :0, ll, ytit, idens[ii], headerbit, lg, (ii==0&&!kmean), col[ii],msty[ii], lsty[ii]);
    }
  }

  if(kmean){
    drawGr(c0, c1, idmean, idmean, ll, ytit, Form("%s, %saveraged,", lg->GetHeader(), gETA>=0?"":"#eta-"),1, lg, kFALSE /*kTRUE*/, kGray+2, 27, kSolid);
    //drawGr(c0, c1, idmean, idmean, ll, ytit, Form("%s, #eta-averaged,", lg->GetHeader()),1, lg, kTRUE, kGray+2, 27, kSolid);
  }

  printf("position 6 drawGr done!\n");
  
  c0->cd();
  TF1 * f0=new TF1("f0","0",0,100); if(glist) glist->Add(f0);
  style::ResetStyle(f0);
  f0->SetLineStyle(kSolid);
  f0->SetLineWidth(1);
  f0->SetLineColor(kBlack);
  f0->Draw("same");

  lg->Draw();

  //rename mean for further use
  ((TGraphAsymmErrors *)ll->At(idmean*2))->SetName(StatName(ipar));
  ((TGraphAsymmErrors *)ll->At(idmean*2+1))->SetName(SysName(ipar));
  ((TGraphAsymmErrors *)ll->At(idmean*2))->SetTitle("averaged");
  ((TGraphAsymmErrors *)ll->At(idmean*2+1))->SetTitle("averaged");


  cc->Print(Form("%s_%s_logx%d_logy%d_%d%s.eps", tag.Data(), outname[ipar].Data(), fklogx, fklogy, fgrid, gETA>=0?Form("_gETA%d", gETA):""));
  cc->Print(Form("%s_%s_logx%d_logy%d_%d%s.png", tag.Data(), outname[ipar].Data(), fklogx, fklogy, fgrid, gETA>=0?Form("_gETA%d", gETA):""));
  cc->Print(Form("%s_%s_logx%d_logy%d_%d%s.root", tag.Data(), outname[ipar].Data(), fklogx, fklogy, fgrid, gETA>=0?Form("_gETA%d", gETA):""));

  printf("position 7\n");

  /*
  //draw inverse:

  //========================
  
  style::fgkTextSize = 0.045;
  style::fgkTitleSize = 0.045;
  style::fgkXTitleOffset = 1.5;//1.1;//1.25;
  style::fgkYTitleOffset = 1.4;//1.2;
  

  style::SetGlobalStyle(0);

  TCanvas * c3  = new TCanvas("c3","",600,500);
  //---------------------------------------

  style::PadSetup(c3);
  c3->SetTopMargin(0.03);
  c3->SetLeftMargin(0.14);
  c3->SetRightMargin(0.03);

  c3->SetLogx(fklogx);

  const Int_t nlsize = ll->GetSize();
  Int_t nplot = 0;
  for(Int_t ii=0; ii<nlsize; ii++){
    TGraphAsymmErrors *  grtmp =    (TGraphAsymmErrors *)ll->At(ii);
    const TString itit = grtmp->GetName();
    if(!itit.Contains("Inv"))
      continue;

    TString dopt;
    if(itit.Contains("stat")){
      dopt=nplot?"lp":"alp";
    }
    else if(itit.Contains("sys")){
      dopt="2";
    }
    else{
      printf("wrong itit!! %s\n", itit.Data());exit(1);
    }

    style::ResetStyle(grtmp);
    grtmp->Draw(dopt);
    nplot++;
  }

  printf("position 8\n");
  style::ResetStyle(lg);

  lg->SetX1NDC(0.4);
  lg->SetX2NDC(0.85);
  lg->SetY1NDC(0.58);
  lg->SetY2NDC(0.95);


  lg->Draw();

  c3->Print(Form("InverseRatio_%s_%s_logx%d_logy%d_%d.eps", tag.Data(), outname[ipar].Data(), fklogx, fklogy, fgrid));
  */

  if(fklogx==0){
    TFile *fout=new TFile(Form("Fraction_%s_%s%s.root", tag.Data(), outname[ipar].Data(), gETA>=0?Form("_gETA%d", gETA):""),"recreate"); 
    ll->Write();
    fout->Save();
    fout->Close();
    delete fout;
  }

  if(glist){
    printf("position 8.5\n");
    ll->ls();
    ll->Clear();
    delete ll;
    delete c0;
    delete c1;
    delete cc;
  }
  printf("position 9\n");
}

int main(int argc, char* argv[])
{
  // drawFractionRatio(1 const TString tag, 2 const Int_t ipar, 3 const TString fin0, 4 const TString fin1="", 5 const TString fin2="", 6 const TString fin3="", 7 const TString fin4="", 8 const TString fin5="", 9 const Bool_t kmean=kFALSE, 10 const TString ytit="fit/true", 11 const Int_t headerbit=16, 12 const Bool_t klogx=kTRUE, 13 const Bool_t klogy=kFALSE, const TString fin6="", const TString fin7="", const TString fin8="", const TString fin9="")

  //'drawFractionRatio.C("'$data'",0,"ETA0_'$data'.root","ETA1_'$data'.root","ETA2_'$data'.root","","","",1,"ratio to ref.",3,'$klogx',0)'

  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]); 
  }

  if(argc!=14){
    printf("argc!=14\n"); return 1;
  }

  TH1::AddDirectory(kFALSE); 

  glist = new TList;
  glist->SetOwner(kTRUE);

  const TString pwd=gSystem->pwd();

  if(pwd.Contains("cmpRec_GrID01") || pwd.Contains("cmpRec_GrID02") || pwd.Contains("cmpRec_GrID51") || pwd.Contains("cmpRec_GrID52")){
    for(gETA=0; gETA<=2; gETA++){
      drawFractionRatio(argv[1], atoi(argv[2]),argv[3],argv[4],argv[5],argv[6],argv[7],argv[8], atoi(argv[9]),argv[10], atoi(argv[11]), atoi(argv[12]), atoi(argv[13]));
    }
  }
  else{
    drawFractionRatio(argv[1], atoi(argv[2]),argv[3],argv[4],argv[5],argv[6],argv[7],argv[8], atoi(argv[9]),argv[10], atoi(argv[11]), atoi(argv[12]), atoi(argv[13]));
  }

  printf("tt0\n");
  glist->ls();
  glist->Clear();

  printf("tt1\n");
  delete glist;

  printf("tt2\n");
  return 0;
}
